package com.sinosoft.system.primary.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.system.primary.dao.SysTokenDao;
import com.sinosoft.system.primary.entity.SysToken;

   /**
    * @ClassName SysTokenDaoJdbc实现类
    * @date 2017-2-8 
    * @author shangxp
    */  
@Repository
public class SysTokenDaoJdbc implements SysTokenDao {
	private static Logger log = Logger.getLogger(SysTokenDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertSysToken(SysToken sysToken) {
		String sqlStr = "insert into sys_token ( "
							+" id ,"	
							+" username ,"	
							+" roles ,"	
							+" issuer ,"	
							+" jwt ,"	
							+" nowMillis ,"	
							+" TTLMillis ,"	
							+" ipaddr ,"	
							+"is_del) values( "
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" 0 )";
		log.debug(sqlStr+"\n"+sysToken.toString());
		try{
			Object[] args = {
					sysToken.getId(),
					sysToken.getUsername(),
					sysToken.getRoles(),
					sysToken.getIssuer(),
					sysToken.getJwt(),
					sysToken.getNowmillis(),
					sysToken.getTtlmillis(),
					sysToken.getIpaddr()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+sysToken.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateSysToken(SysToken sysToken) {
		String sqlStr = "update sys_token set "
									+" id = ? , "
									+" username = ? , "
									+" roles = ? , "
									+" issuer = ? , "
									+" jwt = ? , "
									+" nowMillis = ? , "
									+" TTLMillis = ? , "
									+" ipaddr = ?  "
									+" where id = ?";
		log.debug(sqlStr+"\n"+sysToken.toString());
		try{
			Object[] args = {
					sysToken.getId(),
					sysToken.getUsername(),
					sysToken.getRoles(),
					sysToken.getIssuer(),
					sysToken.getJwt(),
					sysToken.getNowmillis(),
					sysToken.getTtlmillis(),
					sysToken.getIpaddr(),
					sysToken.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+sysToken.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteSysToken(int id) {
		String sqlStr = "update sys_token set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public SysToken selectSysToken(int id) {
		String sqlStr = "select * from sys_token where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public SysToken selectSysToken(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_token where is_del =0 " );
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+"=? ");
				args[i] = queryValues.get(i);
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysToken() {
		String sqlStr = "select count(1) from sys_token where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysToken> selectLimitSysToken(int start, int size) {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" username ,";
					sqlStr+=" roles ,";
					sqlStr+=" issuer ,";
					sqlStr+=" jwt ,";
					sqlStr+=" nowMillis ,";
					sqlStr+=" TTLMillis ,";
					sqlStr+=" ipaddr ";
			sqlStr+=" from sys_token where is_del =0  order by id desc limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				size
		};
			return jdbcTemplate.query(sqlStr, args, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysToken> selectAllSysToken() {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" username ,";
					sqlStr+=" roles ,";
					sqlStr+=" issuer ,";
					sqlStr+=" jwt ,";
					sqlStr+=" nowMillis ,";
					sqlStr+=" TTLMillis ,";
					sqlStr+=" ipaddr ";
		 sqlStr+=" from sys_token where is_del =0 order by id desc ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysToken(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select count(1) from sys_token where is_del = 0 ");
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
				args[i] = "%"+queryValues.get(i)+"%";
			}
		}		
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysToken> selectLimitSysToken(List<String> queryKeys,List<Object> queryValues,int start,int size) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" username ,");
					sqlStr.append(" roles ,");
					sqlStr.append(" issuer ,");
					sqlStr.append(" jwt ,");
					sqlStr.append(" nowMillis ,");
					sqlStr.append(" TTLMillis ,");
					sqlStr.append(" ipaddr ");
			sqlStr.append(" from sys_token where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()+2];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
					args[i] = "%"+queryValues.get(i)+"%";
				}
			}
		if(args==null){
			args = new Object[]{start,size};
		}else{
			args[queryKeys.size()] = start;
			args[queryKeys.size()+1] = size;
		}
		sqlStr.append(" order by id desc ");
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysToken> selectAllSysToken(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" username ,");
					sqlStr.append(" roles ,");
					sqlStr.append(" issuer ,");
					sqlStr.append(" jwt ,");
					sqlStr.append(" nowMillis ,");
					sqlStr.append(" TTLMillis ,");
					sqlStr.append(" ipaddr ");
			sqlStr.append("from sys_token where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" like ? ");
					args[i] = "%"+queryValues.get(i)+"%";
				}
			}
		sqlStr.append(" order by id desc ");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new SysTokenMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class SysTokenMapper implements RowMapper<SysToken> {
		@Override
		public SysToken mapRow(ResultSet arg0, int arg1) throws SQLException {
			SysToken sysToken = new SysToken();
			if(arg0!=null){
					sysToken.setId(arg0.getInt("id"));
					sysToken.setUsername(arg0.getString("username"));
					sysToken.setRoles(arg0.getString("roles"));
					sysToken.setIssuer(arg0.getString("issuer"));
					sysToken.setJwt(arg0.getString("jwt"));
					sysToken.setNowmillis(arg0.getString("nowMillis"));
					sysToken.setTtlmillis(arg0.getString("TTLMillis"));
					sysToken.setIpaddr(arg0.getString("ipaddr"));
				}
			return sysToken;
		}  
	}
}

